Looking for some help on this please guys: I have...
# cfml-general
m
Looking for some help on this please guys: I have a checkbox on a form which is passing concatenated values to an action page. e.g. input type="checkbox" name="MatrixID" id="MatrixID" value="[#rs_StateCode#-#rs_Code1#-#rs_Code2#]" The values passed from the above checkbox on the form are : [AL-5-11],[AL-5-22],[AL-5-101] The content between each of the square brackets [] constitutes a set of records which need to be inserted into a database table So for example the values in the first bracket [AL-5-11] constitutes the first record to be inserted into the database table and so on The fields in the database are as follows: 1st record to be inserted: STATE which we need to insert "AL" CODE1 which we need to insert "5" CODE2 which we need to insert "11" 2nd record to be inserted: STATE which we need to insert "AL" CODE1 which we need to insert "5" CODE2 which we need to insert "22" 3rd record to be inserted STATE which we need to insert "AL" CODE1 which we need to insert "5" CODE2 which we need to insert "101" I need to loop over all three records that are in the square brackets and insert them all into the db I have tried ListGetAT but as the values are dynamic I can't stipulate a start value if that makes sense Thanks in advance. Much appreciated
r
listToArray() and then loop over that?
m
Just trying that now funnily enough Rich 😃 <cfscript> // Define our list myList = "#form.MatrixID#"; // By array for (item in listToArray(myList, "-")) { writeOutput(item); } // By listEach() myList.each(function(element, index) { writeOutput(element & " : " & index); }, ","); </cfscript> </cfoutput>
r
And?
You could try listEach() but I believe arrays are more efficient than lists.
m
This is the output ["AL|184|498,AL|5714|2894"]
a
r
I wasn't going to get all funky and use a map, but that works 😉
🤣 1
😁 1
a
I use map because I got lost easily!
🙀 1
🤣 3
m
@aliaspooryorik perfect thats it!! So all I need to do now is loop over that array and insert into db ?
I'm crap with string manipulation things with checkboxes. I seem to have a phobia 😂
a
So first make it array, which is much easier to work with as you can clearly see what each "value" is. Then I just split it up using word characters
\w
(letters or numbers) which is what the reMatchNocase is doing.
🎉 1
m
@aliaspooryorik brilliant. That's why you write books John 😃
a
Or course, you should write a test first with your expected outcome and then write the code to solve it 😄
👍🏼 1
m
Of course absolute;y. Thanks both much appreciated @aliaspooryorik @richard.herbert 🙏
a
What Richard suggested works as well, so go with the one that makes the most sense to you as one day you'll have to revisit the code! (which is where tests are really handy to remind you what it does!)
🙏 1
m
Thanks guys
a
You could do it as a one-liner but that's probably a bit terse!
output = input.listToArray().map((el) => el.reMatchNocase("\w+"));
m
Yeah agree think its more readable. So just so I understand completely... "\w+" this is looking for the characher (i.e. square bracket ?)
a
No
m
ah
a
m
Yes it does!! So to insert them into those values into the database do I just now loop that array ?
a
yeah
m
superb! wonderful. Thanks for the explanation too. Much clearer with the second fiddle 🙂
a
Not sure quite how you want to do it but I'd probably do something like this to make it really clear https://trycf.com/gist/3d92c1eff5ff0c2cc94ed352ae6794be/lucee5?theme=monokai
Assumes you will always have 3 bits of data
m
Yes much neater! There will be four - I forgot to add one but I can modify it to include the fourth one :-)
@aliaspooryorik ah run into a problem. One of the fields - that fourth field I needed has "-" in the ID and its trimming the ID I may have to use colons as separators instead
👍 1
@aliaspooryorik supposed to be 496BDB38-250F-4F61-B241C67A95F091FB but trimming it to 496BDB38
Ive changed it on the form to <td><input type="checkbox" name="LOALicMatrixID" id="LOALicMatrixID" value="[#rs_StateList.StateCode#:#rs_LOAs.LicenseClassCode#:#rs_LOADetail.LineAuthorityCode#:#m.event('agency')#]"></td>
a
Do a fiddle and I can edit it
m
but its still cutting it off
okay cool
a
probably just need to allow
-
in your characters to match
[a-z0-9\-]
I think should do it
or you could say "match anything that is not a :" in your req exp
up to you
m
Fantastic yes that was it 🙂
👍 1
a
first is probably more readable though
m
yes much more readable 🙂
b
I have a question instead. I was just wondering why you would go to all the trouble of gathering together the various pieces of data (as [#rs_StateCode#-#rs_Code1#-#rs_Code2#]) on the form-page in the first place, if they would be needed separately on the action-page (as StateCode, Code1, Code2).
m
@bkbk its based on the checkbox in the variables coming from loops in different sources
e
My suggestion, which comes back to more of a core suggestion than if it's "doable" is instead of your checkboxes getting thrown together as strings, then passed to however you get here. This seems insecure. Instead, give your checkboxes unique values, so you end up with a list of values you can loop over and insert or do something else. You will find this is significantly faster and less resource-intensive than feeding a list of string values, removing a character you do not need, and then turning that into usable data.
Copy code
ie, someCheckboxData = "al,5,11,al,5,22"  , this can be huge, or small as you like, or you can have many checkboxdatas, doesnt matter,  then you can set the delimiter as a comma, ie MyrRcords = ListToArray(someCheckboxData, ",")  then you can trim your data and loop over it,  <cfloop from="1" to="#ArrayLen(MyRecords)#" step="3" index="i">
    <cfset state = UCase(trim(MyRecords[i]))>
    <cfset code1 = trim(MyRecords[i + 1])>
    <cfset code2 = trim(MyRecords[i + 2])>
🙏 1
r
interesting use of regexp to pick up values, I would not even call it 'splitting', as in this case we rather look for "-" sign and split values like we splitting chocolate bar along divider lines 🙂 this regexp is 'cherry picking' values from a List, but choking if value has "-" (listToArray would choke as well)
👍 1
a
@rodyon OP did update the question as the 3rd value was a UUID with
-
in it. So switched the delimiter and tweaked the regexp to handle it. I find
reMatch
is really handy for this sort of thing. Something like https://cffiddle.org/app/file?filepath=d2bb07fe-bad6-4d4e-8f95-a07866dfb358/3ac779ba-1ccb-4045-b684-0ad581a8ce78/677ed508-0cf5-4b6f-b625-0486b0d1536a.cfm
🙏 1
m
if you have control of the checkbox then i would do something like
Copy code
<cfset rs = {stateCode: 'AL', Code1: '5', Code2: 11}>
<input type="checkbox" value="#EncodeForHTMLAttribute(SerializeJSON(rs))#">
And then just in your FORM Action do soemthing like:
Copy code
recArr=DeserializeJSON("[" & FORM.MatrixID & "]")